// src/apis/auth.js
// src/apis/auth.js

import axios from 'axios';

const BASE_URL = 'http://1.117.70.79:9000/api';
const token = localStorage.getItem('token');

// 设置基础api和将每次登陆的token放入请求头，以便之后的请求验证
axios.defaults.baseURL = BASE_URL;
axios.defaults.headers.common['Authorization'] = `Bearer ${token}`;

// 数据概述
export const dataOverview = { getOverview: () => axios.get('/get/record') };

// 管理员登录
export const adminAuth = {
    login: (value) => axios.post('/admins/login', {
        adminaccount: value.account,
        adminpwd: value.password,
    }),
    register: (value) => axios.post('/admins/register', {
        adminaccount: value.account,
        adminpwd: value.password,
        confirmPassword: value.confirmPassword,
        name: value.name,
    }),
};

// 用户信息管理
export const userManagement = {
    getAllUsers: (current, pageSize) => axios.get(`/users/all-users/${current}/${pageSize}`),
    changePassword: (user) => axios.put('/users/user', user),
    getUser: (id) => axios.get(`/users/user/${id}`),
    deleteUser: (id) => axios.delete(`/users/user/${id}`),
};

// 商品分类管理
export const categoryManagement = {
    getAllCategories: (count, pageSize) => axios.get(`/categories/all-categories/${count}/${pageSize}`),
    addCategory: (categoryName) => axios.post('/categories/category', { categoryName }),
    deleteCategory: (id, curToal) => axios.delete(`/categories/category/${id}/${curToal}`)
};


// 广告投放管理
export const adManagement = {
    getAllAds: () => axios.get('/ads/all-ads'),
    addAd: (adCategory, adCompany, adName, file) => {
        const formData = new FormData();
        formData.append('file', file);
        return axios.post('/ads/ad', formData, {
            params: { adCategory, adCompany, adName },
            headers: { 'Content-Type': 'multipart/form-data' }
        });
    },
    changeAdPictures: (id, file) => {
        const formData = new FormData();
        formData.append('file', file);
        return axios.put(`/ads/ad/${id}`, formData, {
            headers: { 'Content-Type': 'multipart/form-data; boundary=<calculated when request is sent>' }
        });
    },
    deleteAd: (id) => axios.delete(`/ads/ad/delete/${id}`)
};

// 商品信息管理
export const productManagement = {
    //默认商品显示
    getAllProducts: (count, pageSize) => axios.post('/commodities/all-commodities', { count, pageSize }),
    //修改商品
    updateProduct: (product) => axios.put('/commodities/commodity', product),
    //条件查询商品
    matchProducts: (params) => axios.post('/commodities/all-commodities', params),
    //获得商品分类
    getProductslist: () => axios.get('/get/category'),
    //删除商品
    deleteProduct: (id) => axios.delete('/delete/commodity', { params: { id } }),
    //添加商品
    addProduct: (categoryId, commodityName, cost, currentPrice, dangerInventory, file, inventory, sellingUnit) => 
    {
        const formData = new FormData();
        formData.append('file', file);
        return axios.post('/commodities/commodity', formData, {
            params: { categoryId, commodityName, cost, currentPrice, dangerInventory, inventory, sellingUnit },
            headers: { 'Content-Type': 'multipart/form-data' }
        });
    },
};

// 订单管理
export const orderManagement = {
    getOrders: (params) => axios.post('/order/information', params),
    searchOrder: (params) => axios.post('/order/information', params ),
};

    // //搜索获得商品信息
    // fetchProducts: (value) => axios.post('/commodities/all-commodities', {
       
    //     count:1,
    //     id:'代餐品类',
    //     pageSize:value.pageSize,
    //     // popularity:value.popularity
    // }),
    
    // addproduct: (categoryId, commodityName, cost, currentPrice, dangerInventory, file, inventory, sellingUnit) => 
    // {
    //     const formData = new FormData(); 
    //     formData.append('file', file);
    //     formData.append('categoryId', categoryId);
    //     formData.append('commodityName', commodityName);
    //     formData.append('cost', cost);
    //     formData.append('currentPrice', currentPrice);
    //     formData.append('dangerInventory', dangerInventory);
    //     formData.append('inventory', inventory);
    //     formData.append('sellingUnit', sellingUnit);
    //     return axios.post('/commodities/commodity', formData, {
    //         headers: {
    //             'Content-Type': 'multipart/form-data'
    //         } 
    //     });
    // },   
   
    // // changeProduct: (categoryId, commodityName, cost, currentPrice, dangerInventory, file, id, inventory, inventoryStatus, popularity, sales, sellingUnit) => {
    // //     // const formData = new FormData();
    // //     // formData.append('file', file);
    // //     // return axios.put('/commodities/commodity', formData, {
    // //     //     params: {
    // //     //         categoryId,
    // //     //         commodityName,
    // //     //         cost,
    // //     //         currentPrice,
    // //     //         dangerInventory,
    // //     //         id,
    // //     //         inventory,
    // //     //         inventoryStatus,
    // //     //         popularity,
    // //     //         sales,
    // //     //         sellingUnit
    // //     //     },
    // //     //     headers: {
    // //     //         'Content-Type': 'application/json'
    // //     //     }
    // //     // });
        
    // // },
    // changeProduct: (product) => {
    //     return axios.put('/commodities/commodity', product, {
    //         headers: {
    //             'Content-Type': 'application/json'
    //         }
    //     });
    // },

    // deleteProduct: (id) => axios.delete('/delete/commodity', { params: { id } }),
    // getProductslist: () => axios.get('/get/category'),
    // }
 




